﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AppSocketService.Account;
using AppSocketService.Alarm;
using AppSocketService.Fence;
using AppSocketService.Vehicle;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BCGPSRESTful.Controllers.Phone.Account.Person
{
    /// <summary>
    /// 个人用户手机端账户注销
    /// </summary>
    [ApiExplorerSettings(GroupName = "PersonApp")]
    [Route("api/[controller]")]
    [ApiController]
    public class PhoneAccountPersonUnRegisterController : PhoneBaseController
    {
        private readonly IAccountPersonService accountPersonService;
        private readonly IAlarmPersonService alarmPersonService;
        private readonly IFencePersonService fencePersonService;
        private readonly IVehiclePersonService vehiclePersonService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountPersonService"></param>
        /// <param name="alarmPersonService"></param>
        /// <param name="fencePersonService"></param>
        /// <param name="vehiclePersonService"></param>
        public PhoneAccountPersonUnRegisterController(IAccountPersonService accountPersonService, IAlarmPersonService alarmPersonService, IFencePersonService fencePersonService, IVehiclePersonService vehiclePersonService)
        {
            this.accountPersonService = accountPersonService;
            this.alarmPersonService = alarmPersonService;
            this.fencePersonService = fencePersonService;
            this.vehiclePersonService = vehiclePersonService;
        }


        #region 注销账户 --- 已登录账户
        /// <summary>
        /// 注销账户 --- 已登录账户
        /// </summary>
        /// <returns></returns>
        [HttpDelete("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public ActionResult<bool> UnRegister()
        {
            var accountDto = accountPersonService.GetCurrentAccount();
            if(accountDto.CompanyId > 0)
            {
                var list = vehiclePersonService.GetVehicleList();
                if (list != null && list.Count > 0)
                    throw new Exception("你的账户是企业账户下的个人账户并且还存在车辆,不能注销!");
                else
                { 
                    return accountPersonService.UnRegisterPersonAccount();
                }
            }
            else
            { 
                fencePersonService.DeleteAll();
                alarmPersonService.DeleteAll();
                vehiclePersonService.DeleteAll();
                return accountPersonService.UnRegisterPersonAccount();
            }
        }
        #endregion

    }
}